package com.qiaofang.housewarehouse.dao.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: 日期时间工具类
 */
public class DateUtils {
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD_T_HH_MM_SS_SSS = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    /**
     * 解析字符串为时间
     */
    public static String parseDateYYYY_MM_DD(String input) throws Exception {
        SimpleDateFormat pr = new SimpleDateFormat(YYYYMMDD);
        SimpleDateFormat fm = new SimpleDateFormat(YYYY_MM_DD);
        return fm.format(pr.parse(input));
    }
    /**
     * 格式化日期
     */
    public static String formatDate(Date date, String format) throws Exception {
        SimpleDateFormat fm = new SimpleDateFormat(format);
        return fm.format(date);
    }

    public static String dayAddNum(String date, int num) throws Exception {
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.setTime(f.parse(date));
        c.add(Calendar.DAY_OF_MONTH, num);
        Date tomorrow = c.getTime();
        return f.format(tomorrow);
    }

    /**
     * 当天开始时间
     * @return
     */
    private static Date getDayStartDate(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY,0);
        todayStart.set(Calendar.MINUTE,0);
        todayStart.set(Calendar.SECOND,0);
        todayStart.set(Calendar.MILLISECOND,0);
        return todayStart.getTime();
    }


    /**
     * 当天结束时间
     * @return
     */
    private static Date getDayEndDate() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    public static String getDayStartTime()throws Exception{
        return formatDate(getDayStartDate(), YYYY_MM_DD_T_HH_MM_SS_SSS);
    }

    public static String getDayEndTime()throws Exception{
        return formatDate(getDayEndDate(), YYYY_MM_DD_T_HH_MM_SS_SSS);
    }



    public static void main(String[] args) throws Exception {
        //System.out.println(formatDate(new Date(), YYYY_MM_DD_HH_MM));
        System.out.println(getDayStartTime());
    }
}
